﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;

using SIPO.Library;
using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Library.DomainObject.Transaction;
using SIPO.Library.Enum;
using SIPO.Library.Interfaces;

namespace SIPO.Library.DataAccess.Administration.Catalog
{
    public partial class recRegistrationStatusRepo : IRepository<recRegistrationStatus, int>
    {
        public object SelectScalar(SQL.Function.Aggregate function, string column, string parameters)
        {
            object _result = null;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sbQuery = new StringBuilder();
                    switch (function)
                    {
                        case SQL.Function.Aggregate.Max:
                            sbQuery.AppendFormat("SELECT MAX({0}) FROM transactions.recRegistration trr inner join {1}", column, Environment.NewLine);
                            sbQuery.AppendFormat("transactions.recSectorRegistration trsr on trr.RegistrationID=trsr.RegistrationID {0}", Environment.NewLine);
                            sbQuery.AppendFormat("inner join master.recSector mr on mr.SectorID=trsr.SectorID {0}", Environment.NewLine);
                            sbQuery.AppendFormat("inner join master.recSubSector mrs on mrs.SubSectorID=trsr.SubSectorID {0}", Environment.NewLine);
                            sbQuery.AppendFormat("inner join master.recSubSubSector mrss on mrss.SubSubSectorID=trsr.SubSubSectorID {0}", Environment.NewLine);
                            sbQuery.AppendFormat("inner join [master].recRegistrationStatus mrr on mrr.RegistrationStatusID=trsr.RegistrationStatusID {0} {1}", parameters, Environment.NewLine);
                            break;
                        case SQL.Function.Aggregate.Min:
                            sbQuery.AppendFormat("SELECT MIN({0}) FROM transactions.recRegistration trr inner join {1}", column, Environment.NewLine);
                            sbQuery.AppendFormat("transactions.recSectorRegistration trsr on trr.RegistrationID=trsr.RegistrationID {0}", Environment.NewLine);
                            sbQuery.AppendFormat("inner join master.recSector mr on mr.SectorID=trsr.SectorID {0}", Environment.NewLine);
                            sbQuery.AppendFormat("inner join master.recSubSector mrs on mrs.SubSectorID=trsr.SubSectorID {0}", Environment.NewLine);
                            sbQuery.AppendFormat("inner join master.recSubSubSector mrss on mrss.SubSubSectorID=trsr.SubSubSectorID {0}", Environment.NewLine);
                            sbQuery.AppendFormat("inner join [master].recRegistrationStatus mrr on mrr.RegistrationStatusID=trsr.RegistrationStatusID {0} {1}", parameters, Environment.NewLine);
                            break;
                        case SQL.Function.Aggregate.Distinct:
                            sbQuery.AppendFormat("SELECT DISTINCT({0}) FROM transactions.recRegistration trr inner join {1}", column, Environment.NewLine);
                            sbQuery.AppendFormat("transactions.recSectorRegistration trsr on trr.RegistrationID=trsr.RegistrationID {0}", Environment.NewLine);
                            sbQuery.AppendFormat("inner join master.recSector mr on mr.SectorID=trsr.SectorID {0}", Environment.NewLine);
                            sbQuery.AppendFormat("inner join master.recSubSector mrs on mrs.SubSectorID=trsr.SubSectorID {0}", Environment.NewLine);
                            sbQuery.AppendFormat("inner join master.recSubSubSector mrss on mrss.SubSubSectorID=trsr.SubSubSectorID {0}", Environment.NewLine);
                            sbQuery.AppendFormat("inner join [master].recRegistrationStatus mrr on mrr.RegistrationStatusID=trsr.RegistrationStatusID {0} {1}", parameters, Environment.NewLine);
                            break;
                        case SQL.Function.Aggregate.Count:
                            sbQuery.AppendFormat("SELECT COUNT({0}) FROM transactions.recRegistration trr inner join {1}", column, Environment.NewLine);
                            sbQuery.AppendFormat("transactions.recSectorRegistration trsr on trr.RegistrationID=trsr.RegistrationID {0}", Environment.NewLine);
                            sbQuery.AppendFormat("inner join master.recSector mr on mr.SectorID=trsr.SectorID {0}", Environment.NewLine);
                            sbQuery.AppendFormat("inner join master.recSubSector mrs on mrs.SubSectorID=trsr.SubSectorID {0}", Environment.NewLine);
                            sbQuery.AppendFormat("inner join master.recSubSubSector mrss on mrss.SubSubSectorID=trsr.SubSubSectorID {0}", Environment.NewLine);
                            sbQuery.AppendFormat("inner join [master].recRegistrationStatus mrr on mrr.RegistrationStatusID=trsr.RegistrationStatusID {0} {1}", parameters, Environment.NewLine);
                            break;
                        case SQL.Function.Aggregate.Sum:
                            sbQuery.AppendFormat("SELECT SUM({0}) FROM transactions.recRegistration trr inner join {1}", column, Environment.NewLine);
                            sbQuery.AppendFormat("transactions.recSectorRegistration trsr on trr.RegistrationID=trsr.RegistrationID {0}", Environment.NewLine);
                            sbQuery.AppendFormat("inner join master.recSector mr on mr.SectorID=trsr.SectorID {0}", Environment.NewLine);
                            sbQuery.AppendFormat("inner join master.recSubSector mrs on mrs.SubSectorID=trsr.SubSectorID {0}", Environment.NewLine);
                            sbQuery.AppendFormat("inner join master.recSubSubSector mrss on mrss.SubSubSectorID=trsr.SubSubSectorID {0}", Environment.NewLine);
                            sbQuery.AppendFormat("inner join [master].recRegistrationStatus mrr on mrr.RegistrationStatusID=trsr.RegistrationStatusID {0} {1}", parameters, Environment.NewLine);
                            break;
                        case SQL.Function.Aggregate.Avg:
                            sbQuery.AppendFormat("SELECT AVG({0}) FROM transactions.recRegistration trr inner join {1}", column, Environment.NewLine);
                            sbQuery.AppendFormat("transactions.recSectorRegistration trsr on trr.RegistrationID=trsr.RegistrationID {0}", Environment.NewLine);
                            sbQuery.AppendFormat("inner join master.recSector mr on mr.SectorID=trsr.SectorID {0}", Environment.NewLine);
                            sbQuery.AppendFormat("inner join master.recSubSector mrs on mrs.SubSectorID=trsr.SubSectorID {0}", Environment.NewLine);
                            sbQuery.AppendFormat("inner join master.recSubSubSector mrss on mrss.SubSubSectorID=trsr.SubSubSectorID {0}", Environment.NewLine);
                            sbQuery.AppendFormat("inner join [master].recRegistrationStatus mrr on mrr.RegistrationStatusID=trsr.RegistrationStatusID {0} {1}", parameters, Environment.NewLine);
                            break;
                        default:
                            // do nothing 
                            break;
                    }

                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        _result = cmd.ExecuteScalar();
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return _result;
        }

        public List<recRegistrationStatus> GetList(int pageSize, int currentPage, string sortName, string sortOrder, string parameters)
        {
            List<recRegistrationStatus> tt = new List<recRegistrationStatus>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    int startRow = ((currentPage - 1) * pageSize) + 1;
                    int endRow = (startRow + pageSize);

                    StringBuilder sbQuery = new StringBuilder();
                    sbQuery.AppendFormat(" WITH result_set AS ");
                    sbQuery.AppendFormat(" ( ");
                    sbQuery.AppendFormat("    SELECT ");
                    sbQuery.AppendFormat("      ROW_NUMBER() OVER (ORDER BY {0} {1}) AS [row_number], ", sortName, sortOrder);
                    sbQuery.AppendFormat("       trr.RegistrationID, mr.SectorName +'-'+ mrs.SubSectorName +'-'+ mrss.SubSubSectorName as jenisUsaha,mrr.RegistrationStatusName ");
                    sbQuery.AppendFormat("    FROM ");
                    sbQuery.AppendFormat("      transactions.recRegistration trr inner join transactions.recSectorRegistration trsr on ");
                    sbQuery.AppendFormat("      trr.RegistrationID=trsr.RegistrationID inner join master.recSector mr on mr.SectorID=trsr.SectorID ");
                    sbQuery.AppendFormat("      inner join master.recSubSector mrs on mrs.SubSectorID=trsr.SubSectorID ");
                    sbQuery.AppendFormat("      inner join master.recSubSubSector mrss on mrss.SubSubSectorID=trsr.SubSubSectorID ");
                    sbQuery.AppendFormat("      inner join [master].recRegistrationStatus mrr on mrr.RegistrationStatusID=trsr.RegistrationStatusID ");
                    sbQuery.AppendFormat("    {0}", parameters);
                    sbQuery.AppendFormat(" ) ");
                    sbQuery.AppendFormat(" SELECT * FROM result_set WHERE [row_number] BETWEEN {0} AND {1} ", startRow, endRow);

                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                recRegistrationStatus t = new recRegistrationStatus(true);
                                t.recRegistration.RegistrationID = dr[recRegistration.Fields.RegistrationID] == DBNull.Value ? 0 : int.Parse(dr[recRegistration.Fields.RegistrationID].ToString());
                                t.RegistrationStatusName = dr[recRegistrationStatus.Fields.RegistrationStatusName] == DBNull.Value ? string.Empty : dr[recRegistrationStatus.Fields.RegistrationStatusName].ToString();
                                t.recSector.SectorName = dr["jenisUsaha"] == DBNull.Value ? string.Empty : dr["jenisUsaha"].ToString();
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }

        public decimal GetInvoiceValue(string InvoieNo)
        {
            SqlConnection conn = new SqlConnection(connectionString);
            SqlCommand GetInvoice = new SqlCommand("SELECT InvoiceValue from transactions.recinvoice where invoiceno='" + InvoieNo + "'", conn);
            try
            {
                conn.Open();
                return Convert.ToDecimal(GetInvoice.ExecuteScalar());
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                if (conn.State == ConnectionState.Open) conn.Close();
            }

        }
       
    }

}
